package com.naiterui.ehp.bs.recommend.bean.mongo;

import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

import lombok.Builder;
import lombok.Data;

/**
 * 用药提醒闹钟明细
 *
 * @author gaoliangliang
 * @date 2019-06-19 15:03
 * @since 1.0.0
 */
@Data
@Builder(toBuilder = true)
public class AssistDrugRemindItem implements Comparable<AssistDrugRemindItem> {

    private Integer hour;
    private Integer minute;
    private Boolean valid;


    public Date getDate() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        int hourNow = calendar.get(Calendar.HOUR_OF_DAY);
        int minuteNow = calendar.get(Calendar.MINUTE);
        calendar.set(Calendar.HOUR_OF_DAY, hour);
        calendar.set(Calendar.MINUTE, minute);
        if (hourNow >= hour && minuteNow >= minute) {
            calendar.add(Calendar.DATE, 1);
        }
        return calendar.getTime();
    }

    @Override
    public int compareTo(AssistDrugRemindItem o) {

        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        int pin = hour * 60 + minute;
        int o1pin = this.hour * 60 + this.minute;
        int o2pin = o.getHour() * 60 + o.getMinute();

        int space1 = o1pin > pin ? o1pin - pin : 24 * 60 - pin + o1pin;
        int space2 = o2pin > pin ? o2pin - pin : 24 * 60 - pin + o2pin;
        return Integer.compare(space1, space2);
    }
}
